package com.define.ssh.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class IDHelper {
	/**
	 * 获取年龄
	 * 
	 * @return String
	 */
	public static String getAge(String idno) {

		String sCurrentYear = "-1";
		String sCurrentDate = "-1";

		try {
			final Date date = new Date();
			final SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
			final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			sCurrentYear = yearFormat.format(date);
			sCurrentDate = dateFormat.format(date);
		} catch (final Exception e) {
			e.fillInStackTrace();
			return "-1";
		}

		final int iCurrentYear = Integer.parseInt(sCurrentYear); // 当前年分
		final int iCurrentDate = Integer.parseInt(sCurrentDate);
		int iBirthDate = getBirthDate(idno).intValue();
		if (iBirthDate != -1) {
			int iAge = iCurrentYear - iBirthDate / 10000;

			iBirthDate += iAge * 10000;
			if (iBirthDate > iCurrentDate) {

				iAge--;
			}

			return Integer.toString(iAge);

		} else {
			return null;
		}
	}

	public static Integer getBirthDate(String idno) {
		try {
			if (idno.length() < 15) {
				return new Integer(-1);
			}
			if (idno.length() > 17) {
				return new Integer(idno.substring(6, 14));
			} else {
				return new Integer("19" + idno.substring(6, 12));
			}
		} catch (final Exception ex) {
			return new Integer(-1);
		}

	}

	// 18转换成15位身份证
	public static String IDCard18to15(String strSfzh18) {
		String strSfzh15 = null;
		try {
			strSfzh15 = strSfzh18.substring(0, 6) + strSfzh18.substring(8, 17);
			return strSfzh15;
		} catch (final Exception ex) {
			return strSfzh18;
		}
	}

	// 15转换成18位身份证
	public static String IDCard15to18(String strSfzh15) {
		String strSfzh17 = null;
		String strSfzh18 = null;

		try {
			char c; // 效验位
			final char XYW[] = { '1', '0', 'X', '9', '8', '7', '6', '5', '4',
					'3', '2' };
			int qmod, he = 0, i; // 加权
			final int Wi[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8,
					4, 2 };
			final int sfz17i[] = new int[17];
			char sfz17c[] = new char[17];

			if (strSfzh15 == null) {
				return "0";
			}
			if (strSfzh15.length() != 15) {
				return strSfzh15;
			}

			strSfzh17 = strSfzh15.substring(0, 6) + "19"
					+ strSfzh15.substring(6);

			sfz17c = strSfzh17.toCharArray(); // 转换数据
			for (i = 0; i < 17; i++) {
				c = sfz17c[i];
				final int n = c;
				sfz17i[i] = n - 48; // 把字符转换为整型
				he = he + Wi[i] * sfz17i[i];
			}

			final int modq = he / 11;
			qmod = he - modq * 11;
			strSfzh18 = strSfzh17 + String.valueOf(XYW[qmod]);

			return strSfzh18;
		} catch (final Exception e) {
			return strSfzh15;
		}
	}

	public static String formatSFZ(String strIdNo) {

		String strsfz = "-1";

		if (strIdNo.length() > 15 && strIdNo.length() < 18) {

			if ((strIdNo.startsWith("?") || strIdNo.toUpperCase().startsWith(
					"Y"))
					&& strIdNo.length() > 15) {
				strsfz = strIdNo.substring(1, 16);
			} else {
				strsfz = strIdNo.substring(0, 15);
			}
		} else {
			strsfz = strIdNo;
		}

		return strsfz;

	}

}
